/*
 * FileName：Util.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.koine.common.util;

import java.security.SecureRandom;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;


/**
 * @author : hermit
 */
public class Util
{
  private static SecureRandom random = new SecureRandom();
  private static AtomicLong atomicLongID = new AtomicLong(10000L);

  public static String uuid()
  {
    return UUID.randomUUID().toString();
  }

  public static String getUUID()
  {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }

  public static long randomLong()
  {
    return Math.abs(random.nextLong());
  }


  /**
   * 获取一定长度的随机字符串
   * @param length 指定字符串长度
   * @return 一定长度的字符串
   */
  public static String getRandomString(int length) {
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
      int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
    return sb.toString();
  }

  /**
   * 获取id.从DB中取出当前最大的获取id, 然后+1
   *
   * @param parentId
   * @param maxId
   * @return
   */
  public static String getId(String parentId, String maxId) {

    String id = "";

    if (parentId != null && !parentId.equals("")) {

      if ("0".equals(parentId)) {
        parentId = "";
      }

      if (maxId == null || maxId.equals("")) {

        maxId = "0";
      }

      int tmpid = Integer.parseInt(maxId);

      if (tmpid < 9) {

        tmpid++;
        id = parentId + "00" + tmpid;
      } else if (tmpid < 99) {

        tmpid++;
        id = parentId + "0" + tmpid;
      } else {
        tmpid++;
        id = parentId + tmpid;
      }
    }

    return id;
  }

  //随机生成 num位数字
  public static String bulidMobileCode(int num){

    StringBuffer backStr = new StringBuffer();
    Random rand = new Random();
    for(int i=0;i<num;i++){
      backStr.append(rand.nextInt(10));
    }
    return backStr.toString();
  }
}
